package top.maof.book.spider.develop.book.k17;

import lombok.Data;
import top.maof.book.model.Book;
import top.maof.book.model.Contents;
import top.maof.book.spider.develop.Split;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.util.Date;

/**
 * 17K小说网
 */
@Data
@TargetUrl("https://www.17k.com/book/\\d+.html")
@HelpUrl({"https://www.17k.com/", "https://www.17k.com/top/[\\w\\?=/&]*", "https://www.17k.com/all/[\\w\\?=/&]*",
        "https://www.17k.com/man/[\\w\\?=/&]*", "https://www.17k.com/mm/[\\w\\?=/&]*",
        "https://www.17k.com/quanben/[\\w\\?=/&]*"})
public class SeventeenK implements AfterExtractor, Split {

    public static final String CHARSET = "utf-8";

    @ExtractBy("//div[@class=Info]/h1/a/text()")
    private String name;

    @ExtractBy("//div[@class=AuthorInfo]/div[@class=author]/a[@class=name]/text()")
    private String author;

    @ExtractBy("//div[@id=bookCover]/a/img/@src")
    private String img;

    @ExtractBy("//div[@class=infoPath]/div/a[3]/text()")
    private String classify;

    @ExtractBy("//div[@class=Info]/div[@class=label]/a[1]/span/text()")
    private String state;

    /**
     * 书收录时间
     */
    private Date insertDate = new Date();

    /**
     * 最新章节更新时间
     */
    private Date updateDate;

    /**
     * 书源站点名字
     */
    private String sourceSite;

    /**
     * 书源站点地址
     */
    private String sourceUrl;

    /**
     * 书开始连载时间
     */
    private Date startDate;

    @ExtractBy("//p[@class=intro]/a/text()")
    private String synopsis;


    @Override
    public Book getBook() {
        return new Book(null, name, author, img, classify, state,
                insertDate, updateDate, sourceSite, sourceUrl, startDate, synopsis);
    }

    @Override
    public Contents getContents() {
        return null;
    }

    @Override
    public void afterProcess(Page page) {
        this.sourceSite = "17K小说网";
        this.sourceUrl = page.getUrl().toString();

        if ("连载小说".equals(state)) state = "连载";
        else if ("完本小说".equals(state)) state = "完本";

        this.name = this.name.trim();
        this.author = this.author.trim();
        this.synopsis = this.synopsis.trim();
    }
}
